package com.res.share.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.res.share.common.Constants;
import com.res.share.model.ResMessage;
import com.res.share.model.ResReceive;
import com.res.share.model.ResSend;
import com.res.share.model.ResUser;
import com.res.share.mapper.ResUserMapper;
import com.res.share.model.dto.MessageModel;
import com.res.share.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-13
 */
@Service
public class ResUserServiceImpl extends ServiceImpl<ResUserMapper, ResUser> implements IResUserService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private IResUserScoreService resUserScoreService;

    @Autowired
    private IResStatisticsService resStatisticsService;

    @Autowired
    private IResSendService resSendService;

    @Autowired
    private IResMessageService resMessageService;

    @Autowired
    private IResReceiveService resReceiveService;

    @Autowired
    private IResNoticeService resNoticeService;


    @Override
    public ResUser findCurrentUserDetails(HttpServletRequest request, HttpServletResponse response) {
        ResUser resUser = null;
        Integer userId=null;
        Object attribute = request.getSession().getAttribute(Constants.CURRENT_USER);
        if (attribute != null && StringUtils.isNotEmpty(attribute.toString())) {
            userId = Integer.valueOf(attribute.toString());
        }else {
            resUser=null;
        }
        if(userId!=null){
            Object commonCache = cacheService.getCommonCache(Constants.CURRENT_USER_DETAIL+userId);
            if(commonCache!=null && StringUtils.isNotEmpty(commonCache.toString())){
                resUser=JSON.parseObject(commonCache.toString(),ResUser.class);
            }else {
                resUser=findCurrentUserInfo(userId);
                if(resUser!=null){
                    cacheService.setCommonCache(Constants.CURRENT_USER_DETAIL+userId,JSON.toJSONString(resUser));
                }
            }
        }
        return resUser;
    }

    @Override
    public ResUser findUserDetail(String userName) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("USER_NAME",userName);
        queryWrapper.eq("IS_DEL",0);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public ResUser checkUserExits(String info, Integer type) {
        QueryWrapper queryWrapper=new QueryWrapper();

        if(type==1){
            queryWrapper.eq("EMAIL",info);
        }else {
            queryWrapper.eq("USER_NAME",info);
        }
        queryWrapper.eq("IS_DEL",0);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public int insertUser(ResUser saveUserModel) {
        return baseMapper.insert(saveUserModel);
    }

    @Override
    public ResUser findCurrentUserInfo(Integer userId) {
        return baseMapper.selectById(userId);
    }

    @Override
    public void updateUserEmail(Integer userId, String userEmail) {
        baseMapper.updateUserEmail(userId,userEmail);
    }

    @Override
    public void updateUserPassword(String password, Integer userId) {
        baseMapper.updateUserPassword(password,userId);
    }

    @Override
    public Integer userSign(Integer userId) {
        //查询是否签到过
        Integer count=resUserScoreService.findUserSignExits(userId);
        if(count!=null){
            return 2;
        }
        resUserScoreService.saveUserScore(userId);
        resStatisticsService.updateUserScoreNum(userId,1);
        return 1;
    }

    @Override
    public void updateUserMoodsById(Integer beLongUserId) {
        baseMapper.updateUserMoodsById(beLongUserId);
    }

    @Override
    public void updateUserImage(String userImage, Integer userId) {
        baseMapper.updateUserImage(userImage,userId);
    }

    @Override
    public void addMessage(Integer sendUserId, Integer receiveId, String content, String title, Integer type) {
        ResMessage resMessage=new ResMessage();
        resMessage.setMsgType(type);
        resMessage.setMsgTitle(title);
        resMessage.setCreateUserId(sendUserId);
        resMessage.setContent(content);
        boolean save = resMessageService.save(resMessage);
        if(save){
            ResSend resSend=new ResSend();
            resSend.setMessageId(resMessage.getId());
            resSend.setSendId(sendUserId);
            resSend.setCreateUserId(sendUserId);
            resSend.setReceiveId(receiveId);
            resSendService.save(resSend);
            ResReceive resReceive=new ResReceive();
            resReceive.setMessageId(resMessage.getId());
            resReceive.setSendId(sendUserId);
            resReceive.setCreateUserId(sendUserId);
            resReceive.setReceiveId(receiveId);
            resReceive.setIsRead(0);
            resReceiveService.save(resReceive);
        }
    }


    @Override
    public void delMessageNotify(List<String> collect, Integer userId, Integer type) {
        if(type==1){
            resSendService.delsendMessageList(collect,userId);
        }else {
            resReceiveService.delreceiveMessageList(collect,userId);
        }
    }

    @Override
    public ResMessage findMessageByNotifyId(Integer id, Integer type, Integer userId) {
        ResMessage resMessage=null;
        if(type==1){
            //发送者
            ResSend resSend = resSendService.getById(id);
            if(resSend!=null){
                resMessage=resMessageService.getById(resSend.getMessageId());
            }
        }else if(type==2){
            ResReceive resReceive = resReceiveService.getById(id);
            if(resReceive!=null){
                resMessage=resMessageService.getById(resReceive.getMessageId());
                resReceive.setIsRead(1);
                resReceive.setUpdateUserId(userId);
                resReceiveService.updateById(resReceive);
            }
        }else {
            MessageModel systemNoticeDetail = resNoticeService.findSystemNoticeDetail(id);
            if(systemNoticeDetail!=null){
                resMessage=new ResMessage();
                resMessage.setContent(systemNoticeDetail.getContent());
                String status = systemNoticeDetail.getStatus();
                if(status.equalsIgnoreCase("未读")){
                    resNoticeService.insertNotifyData(id,userId);
                }
            }
        }
        return resMessage;
    }

}
